package com.example.sams.controller;


import com.example.sams.domain.request.AddStudentsRequest;
import com.example.sams.domain.request.AddTeachersRequest;
import com.example.sams.domain.request.UpdateStudentsRequest;
import com.example.sams.domain.request.UpdateTeachersRequest;
import com.example.sams.service.IAdminsService;
import com.example.sams.domain.enums.CourseStatus;
import com.example.sams.domain.request.ReviewCourseRequest;
import com.example.sams.domain.request.ChangePasswordRequest;
import com.example.sams.domain.request.UpdateAdminRequest;
import com.example.sams.service.IAdminsService;
import com.example.sams.service.ICourseReviewsService;
import com.example.sams.service.ICoursesService;
import com.example.sams.service.IUsersService;
import com.example.sams.utils.LoginUser;
import com.example.sams.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author jinghong
 * @since 2025-11-04
 */
@RestController
@RequestMapping("/admins")
public class AdminsController {

    @Autowired
    private IAdminsService adminsService;

    @Autowired
    private ICoursesService coursesService;

    @Autowired
    private ICourseReviewsService courseReviewsService;

    @Autowired
    private IAdminsService adminService;

    @Autowired
    private IUsersService usersService;
    /**
     * 添加学生
     * @param addStudentsRequest 添加学生请求参数
     * @return 添加结果
     */
    @PostMapping("/students")
    public ResultUtil addStudents(@RequestBody AddStudentsRequest addStudentsRequest) {
        // 直接调用Service层的login方法，把结果返回给前端（业务逻辑全在Service层）
        return adminService.addStudents(addStudentsRequest);
    }
    /**
     * 添加教师
     * @param addTeachersRequest 添加教师请求参数
     * @return 添加结果
     */
    @PostMapping("/teachers")
    public ResultUtil addTeachers(@RequestBody AddTeachersRequest addTeachersRequest) {
        return adminService.addTeachers(addTeachersRequest);
    }
    /**
     * 获取所有学生信息
     * @return 学生列表
     */
    @GetMapping("/students")
    public ResultUtil getAllStudents() {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return adminService.getAllStudents();
    }
    /**
     * 获取所有教师信息
     * @return 教师列表
     */

    @GetMapping("/teachers")
    public ResultUtil getAllTeachers() {
        return adminService.getAllTeachers();
    }

    /**
     * 更新学生信息
     * @param studentId 学生ID
     * @param request 更新学生信息请求参数
     * @return 更新结果
     */
    @PatchMapping("/students/{studentId}")
    public ResultUtil updateStudents(@PathVariable String studentId, @RequestBody UpdateStudentsRequest request) {
        return adminService.updateStudents(studentId, request);
    }
    
    /**
     * 更新教师信息
     * @param teacherId 教师ID
     * @param request 更新教师信息请求
     * @return 更新结果
     */
    @PatchMapping("/teachers/{teacherId}")
    public ResultUtil updateTeachers(@PathVariable String teacherId, @RequestBody UpdateTeachersRequest request) {
        return adminService.updateTeachers(teacherId, request);
    }

    /**
     * 删除学生信息
     * @param studentId 学生ID
     * @return 删除结果
     */
    @DeleteMapping("/students/{studentId}")
    public ResultUtil deleteStudents(@PathVariable String studentId) {
        return adminService.deleteStudents(studentId);
    }

    /**
     * 删除教师信息
     * @param teacherId 教师ID
     * @return 删除结果
     */
    @DeleteMapping("/teachers/{teacherId}")
    public ResultUtil deleteTeachers(@PathVariable String teacherId) {
        return adminService.deleteTeachers(teacherId);
    }



    /**
     * 15. 获取当前管理员信息
     * GET /admins/me
     */
    @GetMapping("/me")
    public ResultUtil getCurrentAdminInfo() {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return adminsService.getCurrentAdminInfo(userId);
    }

    /**
     * 16. 更新管理员信息
     * PATCH /admins/me
     */
    @PatchMapping("/me")
    public ResultUtil updateAdminInfo(@RequestBody UpdateAdminRequest request) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return adminsService.updateAdminInfo(userId, request);
    }

    /**
     * 17. 修改密码
     * PATCH /admins/me/password
     */
    @PatchMapping("/me/password")
    public ResultUtil changePassword(@RequestBody ChangePasswordRequest request) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return usersService.changePassword(userId, request.getOldPassword(), request.getNewPassword());
    }

    /**
     * 26. 搜索课程
     * GET /admins/courses/search?q={query}
     */
    @GetMapping("/courses/search")
    public ResultUtil searchCourses(@RequestParam(required = false) String q) {
        return coursesService.searchCourses(q);
    }

    /**
     * 27. 审核课程申请
     * POST /admins/course-applications/{applicationId}/review
     */
    @PostMapping("/course-applications/{applicationId}/review")
    public ResultUtil reviewCourseApplication(
            @PathVariable String applicationId,
            @RequestBody ReviewCourseRequest request) {
        String adminId = LoginUser.getCurrentId();
        if (adminId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return courseReviewsService.reviewCourseApplication(applicationId, adminId, request);
    }

    /**
     * 28. 获取课程申请列表
     * GET /admins/course-applications?status={status}
     */
    @GetMapping("/course-applications")
    public ResultUtil getCourseApplications(@RequestParam(required = false) String status) {
        CourseStatus courseStatus = null;
        if (status != null && !status.trim().isEmpty()) {
            try {
                courseStatus = CourseStatus.valueOf(status.toLowerCase());
            } catch (IllegalArgumentException e) {
                return ResultUtil.error("无效的状态参数");
            }
        }
        return courseReviewsService.getCourseApplications(courseStatus);
    }
}
